<?php

namespace app\api\module;

use app\api\model\UserException;
use app\common\model\Goods;
use app\common\model\GoodsCar;
use app\common\model\GoodsComment;
use app\common\model\Category;
use app\common\model\Merch;
use app\common\model\UserCollection;
use app\common\model\UserFocus;
use think\facade\Db;

class Shop extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
        //不需登录的接口
        $ignoreList = [
            ZFN_SHOP_INDEX_GOODS,
            WJH_SHOP_TOP,
            ZFN_SHOP_GOODS,
            ZFN_SHOP_SEARCH_GROUP_GOODS,
            WJH_SHOP_SEARCH_THING_GOODS,
            ZFN_SHOP_GROUP_TOP,
            ZFN_SHOP_COIN_TOP,
            ZFN_SHOP_COIN_GOODS,
            ZFN_SHOP_LIKE_GROUP,
            WJH_SHOP_LIKE_THING,
            ZFN_SHOP_THING_CAR_NUM,
            ZFN_SHOP_GOODS_DETAIL,
            ZFN_SHOP_GOODS_COMMENT,
            ZFN_SHOP_THING_MERCH,
            ZFN_SHOP_THING_MERCH_GOODS,
            ZFN_SHOP_GROUP_MERCH_GOODS,
        ];

        if (in_array($interface, $ignoreList)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return array|string
     */
    public function exec($interface)
    {
        switch ($interface) {
            //【1001】项目最首页的商品推荐
            case ZFN_SHOP_INDEX_GOODS:
                return $this->shopIndexGoods();
            //【1002】商城首页顶部
            case WJH_SHOP_TOP:
                return $this->shopTop();
            //【1003】商城首页商品列表
            case ZFN_SHOP_GOODS:
                return $this->shopGoods();
            //【1004】团购搜索或分类商品列表
            case ZFN_SHOP_SEARCH_GROUP_GOODS:
                return $this->searchGroup();
            //【1005】好物搜索或分类商品列表
            case WJH_SHOP_SEARCH_THING_GOODS:
                return $this->searchThing();
            //【1006】团购商城顶部
            case ZFN_SHOP_GROUP_TOP:
                return $this->groupTop();
            //【1007】积分商城顶部
            case ZFN_SHOP_COIN_TOP:
                return $this->coinTop();
            //【1008】积分商品列表
            case ZFN_SHOP_COIN_GOODS:
                return $this->coinGoods();
            //【1009】猜你喜欢团购
            case ZFN_SHOP_LIKE_GROUP:
                return $this->likeGroup();
            //【1010】猜你喜欢好物
            case WJH_SHOP_LIKE_THING:
                return $this->likeThing();
            //【1011】好物购物车数量
            case ZFN_SHOP_THING_CAR_NUM:
                return $this->thingCarNum();
            //【1012】商品详情
            case ZFN_SHOP_GOODS_DETAIL:
                return $this->goodsDetail();
            //【1013】商品分享
            case ZFN_SHOP_GOODS_SHARE:
                return $this->goodsShare();
            //【1014】商品评论列表
            case ZFN_SHOP_GOODS_COMMENT:
                return $this->goodsComment();
            //【1015】商品收藏和取消
            case ZFN_SHOP_GOODS_COLLECTION:
                return $this->goodsCollection();
            //【1016】购物车加减操作
            case ZFN_SHOP_GOODS_CAR_DO:
                return $this->goodsCarDo();
            //【1017】好物购物车
            case ZFN_SHOP_THING_CAR:
                return $this->thingCar();
            //【1018】购物车删除
            case ZFN_SHOP_CAR_DELETE:
                return $this->carDelete();
            //【1019】好物商家详情
            case ZFN_SHOP_THING_MERCH:
                return $this->thingMerch();
            //【1020】好物商家店内商品搜索
            case ZFN_SHOP_THING_MERCH_GOODS:
                return $this->thingMerchGoods();
            //【1021】关注商家及取消
            case ZFN_SHOP_MERCH_FOCUS:
                return $this->merchFocus();
            //【1022】团购商家详情
            case ZFN_SHOP_GROUP_MERCH:
                return $this->groupMerch();
            //【1023】团购商家店内商品搜索
            case ZFN_SHOP_GROUP_MERCH_GOODS:
                return $this->groupMerchGoods();
            //【1024】获取商家内购物车
            case ZFN_SHOP_MERCH_CAR:
                return $this->merchCar();
            //【1025】商家购物车清空
            case ZFN_SHOP_MERCH_CAR_ClEAR:
                return $this->merchCarClear();
            //【1026】商品获取规格及购物车数量
            case ZFN_SHOP_GOODS_SKU:
                return $this->goodsSku();
            //【1027】获取商品分类
            case WJH_SHOP_GOODS_CATEGORY:
                return $this->getcategory();
            //【1028】加入常买
            case WJH_SHOP_GOODS_BUY_OFTEN:
                return $this->buy_often();
            //【1029】常买列表
            case WJH_SHOP_GOODS_BUY_OFTEN_LIST:
                return $this->buy_often_list();
            //【1030】获取商品二级分类
            case WJH_SHOP_CATEGORY:
                return $this->categorylevel();
            //【1031】取消常买
            case WJH_SHOP_GOODS_CANCEL_OFTEN:
                return $this->cancel_often();

            default:
                send_developer_error("模块【商城】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    private function shopIndexGoods()
    {
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Goods::goodThingList('','',0,$page,$page_size,1);
        $this->insertResponse('data',$data);
        return $this->response('项目首页商品推荐');
    }

    private function shopTop()
    {
        $data = Goods::shopTop();
        $this->insertResponse('',$data);
        return $this->response('商城首页顶部');
    }

    private function shopGoods()
    {
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Goods::goodThingList('','',0,$page,$page_size,0,1);
        $this->insertResponse('data',$data);
        return $this->response('商城首页底部商品列表');
    }

    private function searchGroup()
    {
        $city_id = $this->essentialParam('city_id');
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $category = $this->optionalParam('category');
        $keyword = $this->optionalParam('keyword');
        if($keyword==='' && $category===''){
            throw new UserException('搜索关键词和分类不可同时为空');
        }
        $sort = $this->optionalParam('sort');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Goods::goodGroupList($city_id,$longitude,$latitude,$category,$keyword,$sort,$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('搜索或分类团购商品列表');
    }

    private function searchThing()
    {
        $category = $this->optionalParam('category');
        $keyword = $this->optionalParam('keyword');
/*        if($keyword==='' && $category===''){
            throw new UserException('搜索关键词和分类不可同时为空');
        }*/
        $sort = $this->optionalParam('sort');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Goods::goodThingList($category,$keyword,$sort,$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('搜索或分类好物商品列表');
    }

    private function groupTop()
    {
        $city_id = $this->essentialParam('city_id');
        $data = Goods::groupTop($city_id);
        $this->insertResponse('',$data);
        return $this->response('团购页面顶部');
    }

    private function coinTop()
    {
        $city_id = $this->essentialParam('city_id');
        $data = Goods::coinTop($city_id);
        $this->insertResponse('',$data);
        return $this->response('积分商城顶部');
    }

    private function coinGoods()
    {
        $city_id = $this->essentialParam('city_id');
        $category = $this->optionalParam('category');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Goods::coinGoods($city_id,$category,$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('积分商品列表');
    }

    private function likeGroup()
    {
        $city_id = $this->essentialParam('city_id');
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $activity_type = $this->optionalParam('activity_type');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Goods::likeGroupList($city_id,$longitude,$latitude,$activity_type,$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('团购你可能喜欢推荐');
    }

    private function likeThing()
    {
        $activity_type = $this->optionalParam('activity_type');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Goods::likeThingList($activity_type,$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('好物你可能喜欢推荐');
    }

    private function thingCarNum()
    {
        $user = $this->currentUser(false);
        $data = GoodsCar::thingCarNum($user);
        $this->insertResponse('',$data);
        return $this->response('好物购物车数量');
    }

    private function goodsDetail()
    {
        $user = $this->currentUser(false);
       // $id = input('id');
      //  error_log(json_encode(input()),3,$_SERVER['DOCUMENT_ROOT'].'/wechat.txt');
     //   if(!empty($id)){
            $id = $this->essentialParam('id');
       /* }else{
            $id = 12;
        }
        */
        $source = $this->essentialParam('source');
        $data = Goods::detail($user,$id,$source);
        $this->insertResponse('',$data);
        return $this->response('商品详情');
    }

    private function goodsShare()
    {
        $user = $this->currentUser();
        $goods_id = $this->essentialParam('goods_id');
        $data = Goods::goodsShare($user,$goods_id);
        $this->insertResponse('',$data);
        return $this->response('商品分享');
    }

    private function goodsComment()
    {
        $goods_id = $this->essentialParam('goods_id');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = GoodsComment::lists($goods_id,'','',$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('商品评论列表');
    }

    private function goodsCollection()
    {
        $user = $this->currentUser();
        $goods_id = $this->essentialParam('goods_id');
        $collection_do = $this->essentialParam('collection_do');
        $info = UserCollection::collection($user,$goods_id,$collection_do);
        return $this->response($info);
    }

    private function goodsCarDo()
    {
        $user = $this->currentUser();
        $goods_id = $this->essentialParam('goods_id');
        $sku = $this->optionalParam('sku');
        $car_do = $this->essentialParam('car_do');
        $num = $this->essentialParam('num');
        $info = GoodsCar::do($user,$goods_id,$sku,$car_do,$num);
        return $this->response($info);
    }

    private function thingCar()
    {
        $user = $this->currentUser();
        $data = GoodsCar::thingCar($user);
        $this->insertResponse('',$data);
        return $this->response('总购物车');
    }

    private function carDelete()
    {
        $user = $this->currentUser();
        $goods_car_id = $this->essentialParam('goods_car_id');
        GoodsCar::carDelete($user,$goods_car_id);
        return $this->response('删除成功');
    }

    private function thingMerch()
    {
        $user = $this->currentUser(false);
        $merch_id = $this->essentialParam('merch_id');
        $data = Merch::thingDetail($user,$merch_id);
        $this->insertResponse('',$data);
        return $this->response('好物商家详情');
    }

    private function thingMerchGoods()
    {
        $merch_id = $this->essentialParam('merch_id');
        $keyword = $this->optionalParam('keyword');
        $sort = $this->optionalParam('sort');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Goods::thingMerchGoods($merch_id,$keyword,$sort,$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('好物商家店内商品搜索');
    }

    private function merchFocus()
    {
        $user = $this->currentUser();
        $merch_id = $this->essentialParam('merch_id');
        $focus_do = $this->essentialParam('focus_do');
        $info = UserFocus::focus($user,$merch_id,$focus_do);
        return $this->response($info);
    }

    private function groupMerch()
    {
        $user = $this->currentUser(false);
        $merch_id = $this->essentialParam('merch_id');
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $data = Merch::groupDetail($user,$merch_id,$longitude,$latitude);
        $this->insertResponse('',$data);
        return $this->response('团购商家详情');
    }

    private function groupMerchGoods()
    {
        $merch_id = $this->essentialParam('merch_id');
        $keyword = $this->optionalParam('keyword');
        $sort = $this->optionalParam('sort');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Goods::groupMerchGoods($merch_id,$keyword,$sort,$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('团购商家店内商品列表');
    }

    private function merchCar()
    {
        $user = $this->currentUser(false);
        $merch_id = $this->essentialParam('merch_id');
        $data = GoodsCar::merchCar($user,$merch_id);
        $this->insertResponse('',$data);
        return $this->response('商家内购物车');
    }

    private function merchCarClear()
    {
        $user = $this->currentUser();
        $merch_id = $this->essentialParam('merch_id');
        GoodsCar::merchCarClear($user,$merch_id);
        return $this->response('购物车已清空');
    }

    private function goodsSku()
    {
        $user = $this->currentUser();
        $goods_id = $this->essentialParam('goods_id');
        $source = $this->essentialParam('source');
        $data = Merch::goodsSku($user,$goods_id,$source);
        $this->insertResponse('',$data);
        return $this->response('商品规格及已加购物车数量');
    }
    private function getcategory()
    {

        $data = Category::groupCategory();
        $this->insertResponse('',$data);
        return $this->response('分类列表');
    }


    private function buy_often()
    {
        $user = $this->currentUser();
        $goods_id = $this->essentialParam('goods_id');
        $sku = $this->optionalParam('sku');
        $info = GoodsCar::buy_often_do($user,$goods_id,$sku);
        return $this->response($info);
    }

    private function buy_often_list()
    {
        $user = $this->currentUser();
        $data = GoodsCar::thingBuy_often($user);
        $this->insertResponse('',$data);
        return $this->response('常买');
    }

    private function categorylevel()
    {
        $category_id = $this->essentialParam('category_id');
        $data = Category::getCategoryLevel($category_id,2);
        $this->insertResponse('',$data);
        return $this->response('分类列表');
    }
    private function cancel_often()
    {
        $user = $this->currentUser();
        $goods_id = $this->essentialParam('goods_id');
        $sku = $this->optionalParam('sku');
        $info = GoodsCar::cancel_often_do($user,$goods_id,$sku);
        return $this->response($info);
    }


}